package com.aplana.ccsd.web.template;

import info.magnolia.module.blossom.annotation.Area;
import info.magnolia.module.blossom.annotation.TabFactory;
import info.magnolia.module.blossom.annotation.Template;
import info.magnolia.module.blossom.dialog.TabBuilder;

import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.aplana.ccsd.model.ArticleHeader;
import com.aplana.ccsd.model.MenuItem;
import com.aplana.ccsd.service.ArticleService;
import com.aplana.ccsd.service.NavigationMenuService;

/**
 * Template for article pages.
 */
@Controller
@Template(title = "Aplana Article", id = "blossomCCSD:pages/article")
public class ArticleTemplate {
	
	@SuppressWarnings("unused")
	private static final Logger LOG = LoggerFactory
			.getLogger(ArticleTemplate.class);
	
	@Autowired
    private NavigationMenuService navigationMenuService;
	
	
	@Area("articleHeader")
	@Controller
	public static class ArticleHeaderArea {
		
		@Autowired
		ArticleService articleService;

	    @TabFactory("Content")
	    public void contentTab(TabBuilder tab) {
	    }

	    @RequestMapping("/articleTemplate/header")
	    public String render(Node page, ModelMap model) {
	    	ArticleHeader articleHeader = articleService.getArticleHeader(page);
	    	model.put("articleHeader", articleHeader);
	        return "areas/articleHeader.jsp";
	    }
	}
	
	@Area("articleBody")
	@Controller
	public static class ArticleBodyArea {

	    @TabFactory("Content")
	    public void contentTab(TabBuilder tab) {
	    }

	    @RequestMapping("/articleTemplate/body")
	    public String render(Node page, ModelMap model) {
	        return "areas/verticalArea.jsp";
	    }
	}

    @RequestMapping("/articleTemplate")
    public String render(Node page, ModelMap model) throws RepositoryException {
        List<MenuItem> topMenus = navigationMenuService.getTopMenus(page);
        model.put("topMenus", topMenus);

        return "pages/article.jsp";
    }

    @TabFactory("Content")
    public void propertiesDialog(TabBuilder tab) {
        tab.addEdit("title", "Title", "");
        tab.addCheckbox("hideInNavigation", "Hide in navigation", "Check this box to hide this page in navigation");
    }
}
